package com.gxa.controller;

import com.gxa.common.ResponseData;
import com.gxa.pojo.Supplierz;
import com.gxa.service.SupplierzService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * Created with IntelliJ IDEA.
 * User: zrf
 * Date: 2020/10/26 21:50
 * Description:供应商管理
 * Version: V1.0
 */
@Controller
@RequestMapping("/supplier")
@Api(value = "供应商管理",tags = "供应商管理接口API")
public class SuppliersController {

    @Autowired
    private SupplierzService supplierzService;
    @Autowired
    private ResponseData responseData;

    @RequestMapping(value = "/querySupp",method = RequestMethod.GET)
    @ApiOperation(value = "查询供应商",notes = "查询供应商",httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码数",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示条数",paramType = "query"),
    })
    @ResponseBody
    public ResponseData querySupp(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize){
        System.out.println(currentPage+"  "+pageSize);


        if(supplierzService.querySupp(currentPage,pageSize) != null){
            responseData.setData(supplierzService.querySupp(currentPage,pageSize));
            responseData.setCode(200);
            responseData.setMsg("成功");
        }else{
            responseData.setMsg("失败");
            responseData.setCode(400);
            responseData.setData(null);
        }
        return responseData;
    }

    @RequestMapping(value = "/selSuppBySuppName",method = RequestMethod.GET)
    @ApiOperation(value = "搜索供应商",notes = "根据供应商名称搜索供应商",httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "suppliersName",value = "供应商名称",paramType = "query"),
    })
    @ResponseBody
    public ResponseData selSuppBySuppName(@RequestParam String suppliersName){
        System.out.println(suppliersName);
        if(supplierzService.selSuppByName(suppliersName) != null){
            responseData.setData(supplierzService.selSuppByName(suppliersName));
            responseData.setCode(200);
            responseData.setMsg("成功");
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }

    @RequestMapping(value = "/addSupp",method = RequestMethod.POST)
    @ApiOperation(value = "添加供应商",notes = "添加供应商",httpMethod = "post", response = ResponseData.class)
    @ResponseBody
    public ResponseData addSupp(@RequestBody @ApiParam(name = "supplierz", value = "供应商",required = true) Supplierz supplierz){
        System.out.println(supplierz.toString());

        int result = supplierzService.addSupp(supplierz);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }

    @RequestMapping(value = "/delSupp",method = RequestMethod.GET)
    @ApiOperation(value = "删除供应商",notes = "删除供应商",httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "suppliersId",value = "供应商编码",paramType = "query")
    })
    @ResponseBody
    public ResponseData delSupp(@RequestParam Integer suppliersId){
        System.out.println(suppliersId);
        int result = supplierzService.delSupp(suppliersId);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }


    @RequestMapping(value = "/updateSupp",method = RequestMethod.POST)
    @ApiOperation(value = "编辑供应商",notes = "编辑供应商",httpMethod = "post", response = ResponseData.class)
    @ResponseBody
    public ResponseData updateSupp(@RequestBody @ApiParam(name = "supplierz", value = "供应商",required = true) Supplierz supplierz){
        System.out.println(supplierz.toString());
        int result = supplierzService.updateSupp(supplierz);
        System.out.println(result);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }

        return responseData;
    }
}
